module.exports = function ($resource, $state, $http, $q, FileUploader, $modal, toaster, $stateParams) {
  
	return {
		restrict: 'AE',
		template: require('../views/planningguide.html'),
		replace: true,
		scope: {
			'baseinfo': '=',
			'examineflag' : '='
		},
		link: function (scope, elements, attrs) {
			scope.type = $stateParams.type;
      scope.info = {};
			/* 分页
			* ========================================= */
			scope.maxSize = 5;             //最多显示多少个按钮
			scope.bigCurrentPage = 1;      //当前页码
      scope.itemsPerPage = 10         //每页显示几条
      scope.obj={};
      scope.d={};
      scope.searchform = {};
      var paras = {};
      var url = '';
      if (scope.type == '0') {
        url = '';
        paras = {
          'directory_state' : '1'
        }
      } else if (scope.type == '1') {
        url = '';
        paras = {
          'directory_state' : '2'
        }
      } else if (scope.type == '2') {
        url = '';
        paras = {
          'directory_state' : '4'
        }
      }
			scope.state = '';
			scope.getlist = function (state) {
        scope.state = state;
        var para = {
					pageNo:scope.bigCurrentPage, 
          pageSize:scope.itemsPerPage
        };
        console.log('state' + scope.state);
        if (state == undefined || state == '') { //正常加载 
				  para = angular.extend(paras, para, scope.searchform);
        } else {
            if (scope.type == '1') {
              var para1 = {
                deputy_director_state : state,
              }
              para = angular.extend(para, scope.searchform, para1);
            } else if (scope.type == '2'){
              var para1 = {
                leader_audit_state : state,
              }
              para = angular.extend(para, scope.searchform, para1);
            }
        }
				
				$resource('/api/as/ggc/reviewGuide/findInfoDirectoryList', {}, {}).
				save(para,function(res) {
					console.log(para);
					if (res.errcode !== 0) {
						toaster.error({title:"",body:res.errmsg});
						return;
					}
					
					scope.objs = res.data.results;
					scope.bigTotalItems = res.data.totalRecord;
					console.log(scope.objs);
					console.log('上面是认定指南返回值');				
					angular.forEach(scope.objs,function(item){
						if(item.directory_state == '0'){
							item.directory_state = '草稿'
						}
						if(item.directory_state == '1'){
							item.directory_state = '主要领导审核'
						}
						if(item.directory_state == '2'){
							item.directory_state = '待主管领导审核'
						}
						if(item.directory_state == '3'){
							item.directory_state = '主要领导审核驳回'
						}
						if(item.directory_state == '4'){
							item.directory_state = '主管领导审核通过'
            }
            if(item.directory_state == '5'){
							item.directory_state = '主管领导审核驳回'
						}
						if(item.directory_state == '6'){
							item.directory_state = '主要领导审核通过'
            }
            if(item.directory_state == '7'){
							item.directory_state = '主要领导审核驳回'
						}
						if(item.directory_state == '8'){
							item.directory_state = '下线'
						}
					})	
				})
			};
			
			scope.getlist(); 

			// scope.$watch('examineflag', function(){
			// 	scope.getlist();
			// }, true)
			
			// scope.getlist(); 

			scope.pass = function(info) {
        if (scope.type == '0') {
          var url = '/api/as/ggc/reviewGuide/updateDirsUp';
        } else if (scope.type == '1') {
          var url = '/api/as/ggc/reviewGuide/updateDirssUp';
        } else if (scope.type == '2') {
          var url = '/api/as/ggc/reviewGuide/updateDirLeaUp';
        }
        if (confirm('确定要通过吗?')){
            $resource(url, {}, {}).
            save({'id':info.id},function(res) {
              if (res.errcode != 0) {
                toaster.error({title:"通过失败",body:res.errmsg});
                return;
              } 
                toaster.success({title:"同意成功",body:'已同意'});
                scope.getlist();
              
            });
        }
				scope.getlist();
			}
			scope.back= function (info) {
        scope.flag = 'tuihui'
				var modalInstance = $modal.open({
					template: require('../views/reject_notice.html'),
					controller: 'reject_notices',
					size: 'xs',
					resolve: {
						info: function () {
							return info;
            },
            flag: function () {
							return scope.flag;
						}
					}
				});
				// modalInstance.result.then(function (showResult) {	
        //   scope.examineflag++;
				// });
				//关闭模态框刷新页面
        modalInstance.result.then(function() {
          scope.getlist();
        });
			}
			scope.toinfo = function (index) {
          $state.go('app.guide_cognizance_infos', {'directory_pdf' : scope.objs[index].directory_pdf, 'type' : scope.type, 'id' : scope.objs[index].id, 'state' : scope.state});
          // $state.go('app.projectGuideInfo');
				// console.log(id)
				// var modalInstance = $modal.open({
				// 	template: require('../views/projectGuideInfo.html'),
				// 	controller: 'projectGuideInfo',
				// 	size: 'lg',
				// 	resolve: {
				// 		id: function () {
				// 			return id;
				// 		},
				// 		flag: function () {
				// 			return '23';
				// 		}
				// 	}
				// });
				// modalInstance.result.then(function() {
				// 	scope.getlist();
				// });
			}


    }
	}
}